<template>
  <el-dialog v-model="visible" :title="!dataForm.companyname ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
          <el-form-item label="平台" prop="platform">
        <el-input v-model="dataForm.platform" placeholder="平台"></el-input>
      </el-form-item>
          <el-form-item label="总部" prop="headoffice">
        <el-input v-model="dataForm.headoffice" placeholder="总部"></el-input>
      </el-form-item>
          <el-form-item label="网站" prop="website">
        <el-input v-model="dataForm.website" placeholder="网站"></el-input>
      </el-form-item>
          <el-form-item label="传真" prop="fax">
        <el-input v-model="dataForm.fax" placeholder="传真"></el-input>
      </el-form-item>
          <el-form-item label="服务" prop="services">
        <el-input v-model="dataForm.services" placeholder="服务"></el-input>
      </el-form-item>
          <el-form-item label="分部" prop="branches">
        <el-input v-model="dataForm.branches" placeholder="分部"></el-input>
      </el-form-item>
          <el-form-item label="公司注册编号" prop="companyregisnumber">
        <el-input v-model="dataForm.companyregisnumber" placeholder="公司注册编号"></el-input>
      </el-form-item>
          <el-form-item label="年收入" prop="turnover">
        <el-input v-model="dataForm.turnover" placeholder="年收入"></el-input>
      </el-form-item>
          <el-form-item label="员工数" prop="numberofemployees">
        <el-input v-model="dataForm.numberofemployees" placeholder="员工数"></el-input>
      </el-form-item>
          <el-form-item label="公司建立年份" prop="yearofestablishment">
        <el-input v-model="dataForm.yearofestablishment" placeholder="公司建立年份"></el-input>
      </el-form-item>
          <el-form-item label="注册平台时间" prop="membersince">
        <el-input v-model="dataForm.membersince" placeholder="注册平台时间"></el-input>
      </el-form-item>
          <el-form-item label="有效时间" prop="expirydate">
        <el-input v-model="dataForm.expirydate" placeholder="有效时间"></el-input>
      </el-form-item>
          <el-form-item label="国家" prop="country">
        <el-input v-model="dataForm.country" placeholder="国家"></el-input>
      </el-form-item>
          <el-form-item label="城市" prop="city">
        <el-input v-model="dataForm.city" placeholder="城市"></el-input>
      </el-form-item>
          <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
          <el-form-item label="简介" prop="information">
        <el-input v-model="dataForm.information" placeholder="简介"></el-input>
      </el-form-item>
          <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址"></el-input>
      </el-form-item>
          <el-form-item label="已爬取" prop="crawled">
        <el-input v-model="dataForm.crawled" placeholder="已爬取"></el-input>
      </el-form-item>
          <el-form-item label="销售员" prop="salesperson">
        <el-input v-model="dataForm.salesperson" placeholder="销售员"></el-input>
      </el-form-item>
          <el-form-item label="锁定" prop="tracklock">
        <el-input v-model="dataForm.tracklock" placeholder="锁定"></el-input>
      </el-form-item>
          <el-form-item label="所属大洲" prop="continent">
        <el-input v-model="dataForm.continent" placeholder="所属大洲"></el-input>
      </el-form-item>
      </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  companyname: '',  platform: '',  headoffice: '',  website: '',  fax: '',  services: '',  branches: '',  companyregisnumber: '',  turnover: '',  numberofemployees: '',  yearofestablishment: '',  membersince: '',  expirydate: '',  country: '',  city: '',  email: '',  information: '',  address: '',  crawled: '',  salesperson: '',  tracklock: '',  continent: ''});

const rules = ref({
          platform: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          headoffice: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          website: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          fax: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          services: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          branches: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          companyregisnumber: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          turnover: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          numberofemployees: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          yearofestablishment: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          membersince: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          expirydate: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          country: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          city: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          email: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          information: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          address: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          crawled: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          salesperson: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          tracklock: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          continent: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ]
  });

const init = (companyname?: string) => {
  visible.value = true;
  dataForm.companyname = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (companyname) {
    getInfo(companyname);
  }
};

// 获取信息
const getInfo = (companyname: string) => {
  baseService.get("/company/company/" + companyname).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.companyname ? baseService.post : baseService.put)("/company/company", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
